package com.xjoyt.pay.gateway.controllers;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.xjoyt.pay.commons.PayTypeUtils;
import com.xjoyt.pay.dto.AccessParam;
import com.xjoyt.pay.dto.ExecuteResult;
import com.xjoyt.pay.gateway.annotations.RestApi;
import com.xjoyt.pay.gateway.constants.RestApiConstant;
import com.xjoyt.pay.gateway.params.RateParam;
import com.xjoyt.pay.master.interfaces.MerchantContract;
import com.xjoyt.pay.master.po.Distributor;
import com.xjoyt.pay.master.po.Merchant;
import com.xjoyt.pay.master.po.MerchantRate;

@RestController
@RequestMapping("merchant")
public class MerchantController extends GatewayAbstractController
		implements com.xjoyt.pay.gateway.interfaces.MerchantContract {

	@Autowired
	private com.xjoyt.pay.merchant.register.interfaces.MerchantContract merchantRegisterContract;

	@Autowired
	private MerchantContract contract;

	@Autowired
	private com.xjoyt.pay.merchant.register.interfaces.MerchantRateContract registerMerchantRateContract;

	@RestApi(value = RestApiConstant.MERCHANT_GET, name = "获取商户")
	@Override
	public ExecuteResult<Merchant> get(@RequestBody(required = false) AccessParam<?> param) {
		Merchant merchant = getMerchant(param.accessToken);
		return ExecuteResult.ok(contract.get(merchant.id));
	}

	@RestApi(value = RestApiConstant.MERCHANT_GET_RATE, name = "获取商户费率")
	@Override
	public ExecuteResult<List<MerchantRate>> getMerchantRate(@RequestBody(required = false) AccessParam<?> param) {
		Merchant merchant = getMerchant(param.accessToken);
		return ExecuteResult.ok(contract.getMerchantRate(merchant.id));
	}

	@RestApi(value = RestApiConstant.MERCHANT_GET_BY_NO, name = "通过商户号获取商户")
	@Override
	public ExecuteResult<Merchant> getByNo(@RequestBody(required = false) AccessParam<String> param) {
		return ExecuteResult.ok(contract.getByNo(param.param));
	}

	@RestApi(value = RestApiConstant.MERCHANT_GET_BY_CUSTOMER_NO, name = "通过第三方商户号获取商户")
	@Override
	public ExecuteResult<Merchant> getByCustomerNo(@RequestBody(required = false) AccessParam<String> param) {
		Distributor distributor = getDistributor(param.accessToken);
		if (distributor == null)
			return ExecuteResult.fail("代理商不存在");

		return ExecuteResult.ok(contract.getByDistributorIdAndCustomerNo(distributor.id, param.param));
	}

	@RestApi(value = RestApiConstant.MERCHANT_REGISTER, name = "商户进件")
	@Override
	public ExecuteResult<?> register(
			@RequestBody(required = false) AccessParam<com.xjoyt.pay.merchant.register.po.Merchant> param) {
		Distributor distributor = getDistributor(param.accessToken);
		if (distributor == null)
			return ExecuteResult.fail("代理商不存在");

		param.param.distributor = new com.xjoyt.pay.merchant.register.po.Distributor();
		param.param.distributor.id = distributor.id;
		return merchantRegisterContract.register(param.param);
	}

	@RestApi(value = RestApiConstant.MERCHANT_UPDATE, name = "修改商户")
	@Override
	public ExecuteResult<?> update(
			@RequestBody(required = false) AccessParam<com.xjoyt.pay.merchant.register.po.Merchant> param) {
		Distributor distributor = getDistributor(param.accessToken);
		if (distributor == null)
			return ExecuteResult.fail("代理商不存在");

		param.param.distributor = new com.xjoyt.pay.merchant.register.po.Distributor();
		param.param.distributor.id = distributor.id;
		return merchantRegisterContract.update(param.param);
	}

	@RestApi(value = RestApiConstant.MERCHANT_RESET_KEY, name = "重置密钥")
	@Override
	public ExecuteResult<String> resetKey(@RequestBody(required = false) AccessParam<String> param) {
		Merchant merchant = getMerchant(param.accessToken);
		if (merchant == null)
			return ExecuteResult.fail("商户不存在");

		return contract.resetKey(merchant.id);
	}

	@RestApi(value = RestApiConstant.MERCHANT_OPEN, name = "开通产品")
	@Override
	public ExecuteResult<?> open(@RequestBody(required = false) AccessParam<RateParam> param) {
		if (param.param.rate == null || param.param.rate < 0)
			return ExecuteResult.fail("费率不正确");

		if (StringUtils.isBlank(PayTypeUtils.getName(param.param.type)))
			return ExecuteResult.fail("支付类型不正确");

		Merchant merchant = getMerchant(param.accessToken);
		if (merchant == null)
			return ExecuteResult.fail("商户不存在");

		MerchantRate entity = new MerchantRate();
		entity.merchant = merchant;
		entity.rate = param.param.rate;
		entity.type = param.param.type;
		ExecuteResult<?> result = registerMerchantRateContract.save(entity);
		return result.isSuccess() ? ExecuteResult.ok() : ExecuteResult.fail(result.getMessage());
	}

}
